package com.kt.uokos.yimo.wx.controller;

import com.kt.uokos.yimo.dao.Feedback;
import com.kt.uokos.yimo.service.FeedbackService;
import com.kt.uokos.yimo.util.ResponseUtil;
import com.kt.uokos.yimo.wx.annocation.LoginUser;
import com.kt.uokos.yimo.wx.dao.User;
import com.kt.uokos.yimo.wx.service.UserService;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @description: TODO
 * @className: WxFeedbackController.java
 * @author: Kang Hao
 * @date: 2022/4/25 16:55
 **/
@RestController
@RequestMapping("/wx/feedback")
@Validated
public class WxFeedbackController {

    @Resource
    private FeedbackService feedbackService;

    @Resource
    private UserService userService;

    @PostMapping("/submit")
    public Object submit(@LoginUser Integer userId, @RequestBody Feedback feedback){

        if (userId ==null){
            return ResponseUtil.unlogin();
        }

        if (StringUtils.isEmpty(feedback.getContent())){
            return ResponseUtil.errs();
        }

        if (StringUtils.isEmpty(feedback.getFeedType())){
            return ResponseUtil.errs();
        }

        if (feedback.getHasPicture() == null){
            feedback.setPicUrls(new String[0]);
        }

        if (StringUtils.isEmpty(feedback.getMobile())){
            return ResponseUtil.errs();
        }

        User user = userService.queryById(userId);

        feedback.setId(null);
        feedback.setUserId(userId);
        feedback.setUsername(user.getUsername());
        feedback.setStatus(1);
        feedbackService.add(feedback);
        return ResponseUtil.ok();
    }

}
